package testJdk.stream.statistics;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONUtil;
import org.apache.commons.io.FileUtils;
import org.junit.Test;

import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.LongSummaryStatistics;
import java.util.stream.Collectors;

/**
 * @version 1.0.0
 * Created by cyd on 2022-03-23 15:08:38
 * @ClassName : learn_jdk.stream.statistics.Statistics
 * @Description : 类描述
 */
public class Statistics {
    @Test
    public void test01() throws IOException {
        File file = new File("src/main/java/learn_jdk/stream/statistics/test.txt");
        String str = FileUtils.readFileToString(file, String.valueOf(StandardCharsets.UTF_8));

        JSONArray jsonArray = JSONUtil.parseArray(str);
        List<AgStatisticsDto> agStatisticsDtoList = JSONUtil.toList(jsonArray, AgStatisticsDto.class);

        List<AgStatisticsDto> list = agStatisticsDtoList.stream().filter(agStatisticsDto -> agStatisticsDto.getCostAvg() > 0).collect(Collectors.toList());
        LongSummaryStatistics costAvgStatistics = list.stream().mapToLong(AgStatisticsDto::getCostAvg).summaryStatistics();
        double average = costAvgStatistics.getAverage();
        System.out.println(average);
        list.forEach(agStatisticsDto ->
                System.out.println(agStatisticsDto.getCostAvg()));
    }
}
